package com.yatong.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yatong.commons.util.PageData;
import com.yatong.entity.Company;
import com.yatong.entity.IntentionalCompany;
import com.yatong.service.CompanyService;
import com.yatong.service.IntentionalCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (IntentionalCompany)表控制层
 *
 * @author makejava
 * @since 2018-10-29 21:34:29
 */
@Controller
@RequestMapping("intentionalCompany")
public class IntentionalCompanyController {
    /**
     * 服务对象
     */
    @Autowired
    private IntentionalCompanyService intentionalCompanyService;
    /**
     * 服务对象
     */
    @Autowired
    private CompanyService companyService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public IntentionalCompany selectOne(Integer id) {
        return this.intentionalCompanyService.queryById(id);
    }


    /**
     * 访问意向企业首页
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String intentionalCompany_index(Model model){
        return "intentionalCompany/intentionalCompany_list";
    }


    /**
     * 通过查询条件查询多条数据
     *
     * @param intentionalCompany
     * @return
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public Object queryAll(IntentionalCompany intentionalCompany,@RequestParam Map<String, String> params,
                           @RequestParam(required = false) String param1,
                           @RequestParam(required = false) String param2,
                           @RequestParam(required = false) String param3){

        PageHelper.startPage(Integer.parseInt(params.get("pageNumber")), Integer.parseInt(params.get("pageSize")));
        intentionalCompany.setCompanyName(param1);
        intentionalCompany.setAddress(param2);
        List<IntentionalCompany> companies = this.intentionalCompanyService.queryAll(intentionalCompany,param3);

        PageInfo<IntentionalCompany> page = new PageInfo<IntentionalCompany>(companies);
        return new PageData().ResultMap(page);
    }

    /**
     * 修改企业状态
     * @return
     */
    @RequestMapping("/update_stats")
    @ResponseBody
    public int update_stats(IntentionalCompany intentionalCompany,@RequestParam(required = false) String id,
                            @RequestParam(required = false) String stats){
        int flag = 0;
        intentionalCompany.setId(Integer.parseInt(id));
        intentionalCompany.setCompanyState(stats);
        flag = intentionalCompanyService.update(intentionalCompany);
        return flag;
    }


    /**
     * 访问编辑企业信息页
     * @param model
     * @return
     */
    @RequestMapping("/intentionalCompany_edit")
    public String company_edit(@RequestParam(required = false) String id,Model model){

        IntentionalCompany intentionalCompany = intentionalCompanyService.queryById(Integer.parseInt(id));
        model.addAttribute("intentionalCompany",intentionalCompany);

        Company company = companyService.queryById(intentionalCompany.getCompanyId());
        model.addAttribute("company",company);


        return "intentionalCompany/intentionalCompany_edit";
    }


    /**
     * 修改企业信息
     * @param model
     * @return
     */
    @RequestMapping("/intentionalCompany_update")
    @ResponseBody
    public int company_update(IntentionalCompany intentionalCompany,String intentionalTime,Model model){
        int flag = 0;

        IntentionalCompany thisIntentionalCompany = intentionalCompanyService.queryById(intentionalCompany.getId());
        thisIntentionalCompany.setCompanyName(intentionalCompany.getCompanyName());
        thisIntentionalCompany.setAddress(intentionalCompany.getAddress());
        thisIntentionalCompany.setContacts(intentionalCompany.getContacts());
        thisIntentionalCompany.setTelePhone(intentionalCompany.getTelePhone());
        thisIntentionalCompany.setType(intentionalCompany.getType());
        thisIntentionalCompany.setMobilePhone(intentionalCompany.getMobilePhone());


        if (intentionalTime != null){
            Company company = companyService.queryById(thisIntentionalCompany.getCompanyId());
            company.setIntentionalTime(intentionalTime);
            companyService.updateCompany(company);
        }

        thisIntentionalCompany.setSmallType(intentionalCompany.getSmallType());
        thisIntentionalCompany.setPosition(intentionalCompany.getPosition());
        thisIntentionalCompany.setStaffNumber(intentionalCompany.getStaffNumber());
        thisIntentionalCompany.setDescribes(intentionalCompany.getDescribes());

        flag = intentionalCompanyService.update(thisIntentionalCompany);
        return flag;
    }

}